package com.example.xinyun.slice;

import com.example.xinyun.MyApplication;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.User;
import com.example.xinyun.db.db_User;
import com.example.xinyun.domain.DataBase;
import com.example.xinyun.domain.MD5Util;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

public class LoginSlice extends AbilitySlice {
    static boolean flag=false;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        TextField username=(TextField) findComponentById(ResourceTable.Id_user_textField);
        TextField password=(TextField) findComponentById(ResourceTable.Id_password_textField);
        Text zhuce=(Text) findComponentById(ResourceTable.Id_zhuce);
        Button btn=(Button) findComponentById(ResourceTable.Id_btn_login);
        Checkbox check_password=(Checkbox) findComponentById(ResourceTable.Id_Check_password);
        check_password.setCheckedStateChangedListener((component, state) -> {
            if (state){
                flag=true;
            }else {
                flag=false;
            }
        });
        btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (username.getText()==null||password.getText()==null)
                {
                    new ToastDialog(LoginSlice.this)
                            .setText("用户名或密码为空")
                            .show();
                }else {
                    User user=new User(username.getText(),password.getText(),null);
                    String s1=user.selectuser();
                    ZZRHttp.postJson(UrlContant.URL+"selectUser", user.selectuser(), new ZZRCallBack.CallBackString() {
                        @Override
                        public void onFailure(int i, String s) {
                            new ToastDialog(LoginSlice.this)
                                    .setText("请检查网络连接")
                                    .show();
                        }

                        @Override
                        public void onResponse(String s) {
                            db_User db_user = new Gson().fromJson(s, db_User.class);
                            if (db_user.getCode().equals("200")&&db_user.getData().getPassword().equals(MD5Util.getMD5Str(password.getText())))
                            {
                                if (flag){
                                    DataBase.writeUser("username",db_user.getData().getUser());
                                    //writeUser("password",db_user.getData().getPassword());
                                }
                                MyApplication.username=db_user.getData().getUser();
                                //MyApplication.password=db_user.getData().getPassword();
                                MyApplication.user_Image=db_user.getData().getUserImage();
                                Intent intent=new Intent();
                                //页面跳转
                                present(new HomeFraction(),intent);
                            } else {
                                new ToastDialog(LoginSlice.this)
                                        .setText("用户名或密码输入错误")
                                        .show();
                            }
                        }
                    });
                }
            }
        });
        zhuce.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent=new Intent();
                present(new registerSlice(),intent);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
